package com.lixinyu.service;

import com.lixinyu.pojo.User;
import com.lixinyu.pojo.dto.UserDto;
import com.lixinyu.pojo.dto.UserUpdateDto;
import com.lixinyu.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    @Autowired
    UserRepository userRepository;
    /**
     * 插入用户
     *
     * @param user 参数
     * @return
     */
    @Override
    public User add(UserDto user) {
        //调用数据访问类 repository
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);

        return userRepository.save(userPojo);
    }

    @Override
    public User getUser(Integer userId) {
        return userRepository.findById(userId).orElseThrow(() -> {
            throw new IllegalArgumentException("用户不存在，参数异常！");
        });

    }

    /**
     * 修改用户
     *
     * @param user 参数
     * @return
     */
    @Override
    public User update(UserUpdateDto user) {
        //调用数据访问类 repository
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }

    @Override
    public void delete(Integer userId) {
        userRepository.deleteById(userId);

    }
}
